package com.eazyimage.lab.service;

import com.eazyimage.lab.entity.Dataset;
import com.eazyimage.lab.mapper.DeleteDatasetMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

import static com.eazyimage.lab.service.SH256Service.getSHA256Str;

@Service
public class DeleteDatasetService {
    @Autowired
    DeleteDatasetMapper dd;
    public boolean DeleteDataset(int datasetId){
        try {
            dd.DeleteDataset(datasetId);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static void DeleteDatasetFile(File directory){
        if (!directory.isDirectory()){
            directory.delete();
        } else{
            File [] files = directory.listFiles();

            // 空文件夹
            if (files.length == 0){
                directory.delete();
                //System.out.println("删除" + directory.getAbsolutePath());
                return;
            }

            // 删除子文件夹和子文件
            for (File file : files){
                if (file.isDirectory()){
                    DeleteDatasetFile(file);
                } else {
                    file.delete();
                    //System.out.println("删除" + file.getAbsolutePath());
                }
            }

            // 删除文件夹自己
            directory.delete();
            //System.out.println("删除" + directory.getAbsolutePath());
        }
    }
}

